import { _decorator } from 'cc';
import { eliminateGameManager } from './EliminateGameManager';
import { director } from 'cc';
import { resourceManager } from '../../common/ResourceManager';
import { GameConfig } from '../../GameConfig';
import NDB from '../../common/NDBTS';
import BaseComp from '../../common/BaseComp';
import { Sprite } from 'cc';
import { Label } from 'cc';
import { GameOverReason } from './EliminateGame';
import { Scene } from 'cc';
import { audioManager } from '../../common/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('EliminateResult')
export class EliminateResult extends BaseComp {

    private pic_win: Sprite = null;
    private pic_lose: Sprite = null;

    private bg: Sprite = null;

    private btn: Sprite = null;

    private canClickNext: boolean = true;

    /** 得分 */
    private scoreLabel: Label = null;

    __preload(): void {
        this.openFilter = true;
        super.__preload();
    }

    protected onLoad(): void {
        // const renderTexture = eliminateGameManager.uiManager.camera.targetTexture;
        // let texture = new SpriteFrame();
        // texture.texture = renderTexture;
        // this.bg.getComponent(Sprite).spriteFrame = texture;

        // const mat = this.bg.getComponent(Sprite).customMaterial;
        // mat.setProperty('mainTexture', renderTexture);

        console.log("max Level is ", eliminateGameManager.curLevel, " cur play level is ", eliminateGameManager.curPlayLevel, " time is ", Date.now());
        this.btn.node.active = eliminateGameManager.game.overReason == GameOverReason.NORMAL_OVER && eliminateGameManager.curPlayLevel < eliminateGameManager.curLevel;
    }

    start() {

    }

    public showToast(isWin: boolean, score: number) {
        this.pic_win.node.active = isWin;
        this.pic_lose.node.active = !isWin;

        if (isWin) {
            audioManager.playEffect('audio/win');
            audioManager.playEffect('audio/sound_fireworks_explosion_1');
        }

        this.scoreLabel.string = score + "";
    }

    update(deltaTime: number) {

    }

    btnEvents(event: any, data: any) {
        switch (data) {
            case "next":
                if (!this.canClickNext) return;
                this.canClickNext = false;
                if (eliminateGameManager.game.overReason == GameOverReason.NORMAL_OVER && eliminateGameManager.curPlayLevel < eliminateGameManager.curLevel) {
                    eliminateGameManager.curPlayLevel++;
                    console.log('-----', eliminateGameManager.curPlayLevel);
                }
                resourceManager.loadSceneInBundle(`scene/${GameConfig.gameName}_GameScene`, GameConfig.gameName, () => {
                    director.loadScene(`${GameConfig.gameName}_GameScene`, (err, scene: Scene) => {
                        if (err) {
                            return;
                        }
                        this.canClickNext = true;
                    });
                })
                break;
            case "exit":
                eliminateGameManager.game.leaveGame().then(() => {
                    NDB.closeWebView();
                });
                break;
            case "replay":
                // const overData: EliminateOver = {
                //     type: 'over',
                //     user_id: +userManager.userId,
                //     game_id: eliminateGameManager.gameId,
                //     stop_reason: GameStopReason.FORCE_CLOSE
                // }
                // // 发送游戏结束消息
                // socketManager.sendMessage(overData);
                resourceManager.loadSceneInBundle(`scene/${GameConfig.gameName}_GameScene`, GameConfig.gameName, () => {
                    director.loadScene(`${GameConfig.gameName}_GameScene`, (err, scene: Scene) => {
                        if (err) {
                            return;
                        }
                        this.canClickNext = true;
                    });
                })
                break;
        }
    }
}

